﻿using Lufei_CaterModel;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lufei_CaterDAL
{
    public class OrderInfoDAL
    {

        /// <summary>
        /// 插入订单信息
        /// </summary>
        /// <param name="oi"></param>
        /// <returns></returns>
        public int InsertOrderInfo(OrderInfo oi)
        {
            //last_insert_rowid获取最后一次插入数据的主键/ID；
            string sql = "insert into OrderInfo(subtime,remark,orderstate,delflag,subby,begintime)values(@subtime,@remark,@orderstate,@delflag,@subby,@begintime);select last_insert_rowid()";

            SQLiteParameter[] param=new SQLiteParameter[] {
            new SQLiteParameter("@subtime",oi.SubTime),
            new SQLiteParameter("@remark",oi.Remark),
            new SQLiteParameter("@orderstate",oi.OrderState),
            new SQLiteParameter("@delflag",oi.DelFlag),
            new SQLiteParameter("@subby",oi.SubBy),
            new SQLiteParameter("@begintime",oi.BeginTime),
            };
           return Convert.ToInt32(SqlHelper.ExecuteScalar(sql, param));
        }



        /// <summary>
        /// 根据桌子id查询订单id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetOrderIDByDeskId(int id)
        {
            string sql = "select odr.orderid from orderinfo as odr inner join r_order_desk as rod on odr.orderid=rod.orderid inner join deskinfo as dsi on dsi.deskid=rod.deskid where odr.orderstate=1 and odr.DelFlag=0 and dsi.deskstate=1 and dsi.DelFlag=0 and dsi.deskid=" + id;

            //string sql = "select orderinfo.orderid from orderinfo inner join r_order_desk on orderinfo.orderid=r_order_desk.orderid inner join deskinfo on deskinfo.deskid=r_order_desk.deskid where orderinfo.orderstate=1 and orderinfo.DelFlag=0 and deskinfo.deskstate=1 and deskinfo.DelFlag=0 and deskinfo.deskid="+id;

          return Convert.ToInt32(SqlHelper.ExecuteScalar(sql));

        }


        /// <summary>
        /// 获取钱
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public decimal GetMoney(int orderid)
        {
            string sql = "select orderMoney from orderInfo where orderid=" + orderid;
            return  Convert.ToDecimal(SqlHelper.ExecuteScalar(sql));
        }


        /// <summary>
        /// 更新金额
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="orderMoney"></param>
        /// <returns></returns>
        public int UpdateMoney(int orderID,decimal orderMoney)
        {
            string sql = "update orderinfo set orderMoney=@orderMoney where orderid=@orderid";
            SQLiteParameter[] param = new SQLiteParameter[]
            {
                new SQLiteParameter("@orderMoney",orderMoney),
                new SQLiteParameter("@orderid",orderID),
            };
             return   SqlHelper.ExecuteNonQuery(sql,param);
        }

        /// <summary>
        /// 更新订单表数据
        /// </summary>
        /// <param name="oi"></param>
        /// <returns></returns>
        public int UpdateOrderData(OrderInfo oi)
        {
            string sql = "update orderinfo set OrderState=@OrderState,OrderMemId=@OrderMemId,DelFlag=@DelFlag,EndTime=@EndTime,OrderMoney=@OrderMoney,DisCount=@DisCount where OrderID=@OrderID";

            SQLiteParameter[] param = new SQLiteParameter[]
            {
                new SQLiteParameter("@OrderState",oi.OrderState),
                new SQLiteParameter("@OrderMemId",oi.OrderMemId),
                new SQLiteParameter("@DelFlag",oi.DelFlag),
                new SQLiteParameter("@EndTime",oi.EndTime),
                new SQLiteParameter("@OrderMoney",oi.OrderMoney),
                new SQLiteParameter("@DisCount",oi.DisCount),
                new SQLiteParameter("@OrderID",oi.OrderId),
            };
          return    SqlHelper.ExecuteNonQuery(sql,param);
        }



    }
}
